package com.schoolcolud.daomybatisplus.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.schoolcloud.common.model.PageQuery;
import lombok.Data;

@Data
public class MpPageQuery extends PageQuery {
    private String sortByColumn;
    /**
     * 将自定义分页query转化为MP所需的page对象
     *
     * @return {@link Page}<{@link T}>
     */
    public <T> Page<T> toMpPage() {
        Page<T> page = Page.of(getPageNum(), getPageSize());
        if (StrUtil.isNotBlank(getSortByColumn())) {
            page.addOrder(new OrderItem().setColumn(getSortByColumn()).setAsc(getIsAsc()));
        }
        return page;
    }
}
